package com.ioc.business.order.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ioc.business.order.domain.PlanArrivalOrder;
import com.ioc.business.order.domain.bo.plan.arrival.PlanArrivalOrderBo;
import com.ioc.business.order.domain.bo.plan.arrival.PlanArrivalOrderQueryBo;
import com.ioc.business.order.service.IPlanArrivalOrderService;
import com.ioc.business.order.vo.plan.arrival.PlanArrivalOrderImportVo;
import com.ioc.business.order.vo.plan.arrival.PlanArrivalOrderPageVo;
import com.ioc.business.order.vo.plan.arrival.PlanArrivalOrderVo;
import com.ioc.common.annotation.Log;
import com.ioc.common.core.controller.BaseController;
import com.ioc.common.core.domain.AjaxResult;
import com.ioc.common.core.page.TableDataInfo;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.enums.BusinessType;
import com.ioc.common.utils.DateUtils;
import com.ioc.common.utils.poi.ExcelUtil;
import com.ioc.common.validate.AddGroup;
import com.ioc.common.validate.EditGroup;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 计划到货订单表 前端控制器
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-08
 */
@RestController
@RequestMapping("/plan-arrival-order")
@AllArgsConstructor
public class PlanArrivalOrderController extends BaseController {
    private final IPlanArrivalOrderService planArrivalOrderService;

    /**
     * 分页查询计划到货订单列表
     *
     * @param queryBo
     * @return
     * @folder 计划到货订单/分页查询计划到货订单列表
     */
    @GetMapping("/list")
    public TableDataInfo selectPlanArrivalOrderPageList(PlanArrivalOrderQueryBo queryBo) {
        startPage();
        TableVo<PlanArrivalOrder, PlanArrivalOrderPageVo> data = planArrivalOrderService.selectPlanArrivalOrderPageList(queryBo);
        return getDataTable(data);
    }

    /**
     * 根据日期查询客户名称选项列表
     *
     * @param date
     * @return
     */
    @GetMapping("/options/list")
    public AjaxResult getCustomerNameOptions(String date) {
        List<String> customerNameList = planArrivalOrderService.getCustomerNameOptions(date);
        return success(customerNameList);
    }

    /**
     * 查询计划到货订单详情信息
     *
     * @param planArrivalOrderId
     * @return
     * @folder 计划到货订单/查询计划到货订单详情信息
     */
    @GetMapping("/{planArrivalOrderId}")
    public AjaxResult getPlanArrivalOrderInfo(@PathVariable("planArrivalOrderId") Long planArrivalOrderId) {
        PlanArrivalOrderVo planArrivalOrderVo = planArrivalOrderService.selectPlanArrivalOrderById(planArrivalOrderId);
        if (ObjectUtil.isEmpty(planArrivalOrderVo)) {
            return error("数据不存在!");
        }
        return success(planArrivalOrderVo);
    }

    /**
     * 新增计划到货订单
     *
     * @param bo
     * @return
     * @folder 计划到货订单/新增计划到货订单
     */
    @Log(title = "计划到货订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addPlanArrivalOrder(@RequestBody @Validated(AddGroup.class) PlanArrivalOrderBo bo) {
        if (ObjectUtil.isNotEmpty(bo.getArrivalOrderDate()) &&
                planArrivalOrderService.checkArrivalOrderDate(null, bo.getArrivalOrderDate())) {
            return error("新增失败! 计划到货日期【" + DateUtil.format(bo.getArrivalOrderDate(),
                    DateUtils.YYYY_MM_DD) + "】已存在");
        }
        return toAjax(planArrivalOrderService.addPlanArrivalOrder(bo));
    }

    /**
     * 编辑计划到货订单
     *
     * @param bo
     * @return
     * @folder 计划到货订单/编辑计划到货订单
     */
    @PostMapping("/edit")
    @Log(title = "计划到货订单", businessType = BusinessType.UPDATE)
    public AjaxResult updatePlanArrivalOrder(@RequestBody @Validated(EditGroup.class) PlanArrivalOrderBo bo) {
        if (ObjectUtil.isNotEmpty(bo.getArrivalOrderDate()) &&
                planArrivalOrderService.checkArrivalOrderDate(bo.getPlanArrivalOrderId(), bo.getArrivalOrderDate())) {
            return error("编辑失败! 计划到货日期【" + DateUtil.format(bo.getArrivalOrderDate(),
                    DateUtils.YYYY_MM_DD) + "】已存在");
        }
        return toAjax(planArrivalOrderService.updatePlanArrivalOrder(bo));
    }

    /**
     * 删除计划到货订单
     *
     * @param idList
     * @return
     * @folder 计划到货订单/删除计划到货订单
     */
    @Log(title = "计划到货订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody List<Long> idList) {
        return toAjax(planArrivalOrderService.removePlanArrivalOrder(idList));
    }


    /**
     * 下载计划到货订单模版
     *
     * @param response
     * @folder 计划到货订单/下载计划到货订单模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<PlanArrivalOrderImportVo> util = new ExcelUtil<>(PlanArrivalOrderImportVo.class);
        util.importTemplateExcel(response, "计划到货订单导入模版");
    }

    /**
     * 导入计划到货订单数据
     *
     * @param file
     * @return
     * @throws Exception
     * @folder 计划到货订单/导入计划到货订单数据
     */
    @Log(title = "收货订单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<PlanArrivalOrderImportVo> util = new ExcelUtil<>(PlanArrivalOrderImportVo.class);
        List<PlanArrivalOrderImportVo> voList = util.importExcel(file.getInputStream());
        planArrivalOrderService.importPlanArrivalOrder(voList);
        return success();
    }

}
